package com.mw.wmimobile.gui.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.mw.wmimobile.commons.LoadingDialog;
import com.mw.wmimobile.datastorage.ClassesDto;
import com.mw.wmimobile.datastorage.DataStorage;
import com.wm.wmimobile.R;

public class ClassesFragment extends FragmentActivity {

	private EditText findEditText;
	private ImageView generalButton;
	private ImageView myScheduleButton;
	private ListView listView;
	private List<ClassesDto> classesDtoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ClassesFragment classesFragment = ClassesFragment.this;
		setContentView(R.layout.class_activity_list);

		listView = (ListView) findViewById(R.id.ClassesActivityListView);

		loadData();

		findEditText = (EditText) findViewById(R.id.ClassesFindEditTextView);

		findEditText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String text = s.toString().toLowerCase(Locale.getDefault());
				find(text);
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				String text = s.toString().toLowerCase(Locale.getDefault());
				find(text);
			}
		});

		generalButton = (ImageView) findViewById(R.id.GeneralClassesButton);
		generalButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				classesDtoList = DataStorage.getInstance().getClassesList();
				showData();

			}
		});

		myScheduleButton = (ImageView) findViewById(R.id.MyClassesButton);
		myScheduleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				classesDtoList = DataStorage.getInstance().getMyClassesList();
				if (classesDtoList != null)
					showData();
				// loadData(false);
			}
		});
	}

	private void loadData() {
		final LoadingDialog loadingDialog = new LoadingDialog(this, "wczytuje");
			classesDtoList = DataStorage.getInstance().getClassesList();
			showData();
	}

	public void find(String text) {
		List<ClassesDto> tmpClassesList = new ArrayList<ClassesDto>();
		if (text != null && text.length() > 0) {
			Integer i = 0;
			for (ClassesDto classesDto : DataStorage.getInstance()
					.getClassesList()) {
				if (classesDto.getTeacher().toLowerCase(Locale.getDefault())
						.contains(text)) {
					tmpClassesList.add(classesDto);
					i++;
				} else if (classesDto.getName()
						.toLowerCase(Locale.getDefault()).contains(text)) {
					tmpClassesList.add(classesDto);
					i++;
				} else if (classesDto.getCode()
						.toLowerCase(Locale.getDefault()).contains(text)) {
					tmpClassesList.add(classesDto);
					i++;
				}
			}
			classesDtoList = tmpClassesList;
		} else {
			classesDtoList = DataStorage.getInstance().getClassesList();
		}

		listView.setAdapter(new ClassesAdapter(ClassesFragment.this,
				classesDtoList));
		listView.invalidate();
		//findEditText.requestFocus();
	}

	private void showData() {
		listView.setAdapter(new ClassesAdapter(ClassesFragment.this,
				classesDtoList));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		listView.setAdapter(new ClassesAdapter(ClassesFragment.this,
				classesDtoList));
		listView.invalidate();
	}

}
